package com.dnjn.happiness.survey.controller.common;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.survey.domain.CheckItem;
import com.dnjn.happiness.survey.domain.vo.CheckItemVO;
import com.dnjn.happiness.survey.service.ICheckItemService;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;


/**
 * 考核Controller
 *
 * @author happiness
 * @date 2024-04-07
 */

@Slf4j
public class CommonCheckItemController extends BaseController {
    @Autowired
    protected ICheckItemService checkItemService;

    /**
     * 查询考核列表
     */
    @Log(title = "考核", businessType = BusinessType.LIST)
//    @RequiresPermissions("@ss.hasAnyPermi('check:item:list,ignore:item:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody CheckItemVO vo, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        CheckItem checkItem=new CheckItem();
        BeanUtils.copyPropsIgnoreNull(vo,checkItem);
        Page page = Page.of(current, size);
        Page<CheckItem> list = checkItemService.page(page, Wrappers.query(checkItem));
        return success(list);
    }

    /**
     * 获取考核详细信息
     */
    @Log(title = "考核", businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:item:query,ignore:item:query')")
    @GetMapping(value = "/{checkItemId}")
    public AjaxResult get(@PathVariable("checkItemId") Long checkItemId) {
        return success(checkItemService.getById(checkItemId));
    }

    /**
     * 新增考核
     */
    @Log(title = "考核", businessType = BusinessType.INSERT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:item:add,ignore:item:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CheckItemVO vo) {
        CheckItem checkItem=new CheckItem();
        BeanUtils.copyPropsIgnoreNull(vo,checkItem);

        checkItem.setCreateBy(SecurityUtils.getUsername());
        return toAjax(checkItemService.save(checkItem));
    }

    /**
     * 修改考核
     */
    @Log(title = "考核", businessType = BusinessType.UPDATE)
//    @RequiresPermissions("@ss.hasAnyPermi('check:item:edit,ignore:item:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody CheckItemVO vo) {
        CheckItem checkItem=new CheckItem();
        BeanUtils.copyPropsIgnoreNull(vo,checkItem);
        return toAjax(checkItemService.updateById(checkItem));
    }

    /**
     * 删除考核
     */
    @Log(title = "考核", businessType = BusinessType.DELETE)
//    @RequiresPermissions("@ss.hasAnyPermi('check:item:remove,ignore:item:remove')")
    @DeleteMapping("/{checkItemIds}")
    public AjaxResult remove(@PathVariable Long[] checkItemIds) {
        return toAjax(checkItemService.removeBatchByIds(Arrays.asList(checkItemIds)));
    }

    /**
     * 获取考核详细信息
     */
    @Log(title = "考核", businessType = BusinessType.SELECT)
//    @RequiresPermissions("@ss.hasAnyPermi('check:item:query,ignore:item:query')")
    @GetMapping(value = "/getItemByUserId")
    public AjaxResult getItemByUserId( Long userId) {
        List<CheckItemVO> checkItemList=checkItemService.getItemByUserId(userId);
        return success(checkItemList);
    }
}
